<template>
	<div class="open-map">
		<iframe id="mapPage" width="100%" height="100%" frameborder=0 :src="mapUrl"></iframe>
	</div>
</template>

<script>
import common from '../../utils/common.js';
var app = getApp();
var _this;
export default {
	name: 'openMap',
	data() {
		return {
			searchTxt: '',
			sugguestedList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
			showCityList: false,
			nowMap: {
				// 地图配置
				center: { lng: 117.21098502609193, lat: 31.83229194441236 }, // 地图中心点位置
				zoom: 14 // 地图缩放级别
			},
			currentPosition: {},
			locationList: [], // 定位地址推荐列表
			searchList: [], // 搜索推荐列表
			searchTimer: '',
			mapUrl: '',
			location_type: ''
		};
	},
	onLoad (options) {
		window.addEventListener('message', this.getAddress);
		_this = this;
		common.setMainColor(_this, function(config) {
			console.log('config', config);
			if(config.config.map_config == 'google'){
				_this.mapUrl = location.protocol + '//' + location.host + '/wap.php?c=Gmap&from=uniapp';
			}else{
				//var mapUrl = 'https://apis.map.qq.com/tools/locpicker?search=1&type=1&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77&referer=myapp';
				var mapUrl = location.protocol + '//' + location.host + '/wap.php?c=My&a=adres_map&from=uniapp';
				if (options.coord) {
					// 参考文档 https://lbs.qq.com/tool/component-picker.html
					mapUrl = mapUrl + '&type=1&coord=' + options.coord + '&coordtype=' + options.coordtype
				}
				_this.mapUrl = mapUrl;
			}
		});
		if(options.location_type){
			_this.location_type = 'gcj02'
		}
	},
	onUnload () {
		window.removeEventListener('message', this.getAddress);
	},
	methods: {
		getAddress (event) {
			// 接收位置信息，用户选择确认位置点后选点组件会触发该事件，回传用户的位置信息
			console.log('选中地址', event)
			var loc = event.data;
			if (loc && loc.module == 'locationPicker') {//防止其他应用也会向该页面post信息，需判断module是否为'locationPicker'
			  console.log('location', loc);
			  loc.adress = loc.poiname
			  loc.lng = loc.longitude = loc.latlng.lng
			  loc.lat = loc.latitude = loc.latlng.lat
			  
			  if(_this.location_type == 'gcj02'){
				  common.post('Map&a=baiduToGcj02', {baidu_lat: loc.lat, baidu_lng: loc.lng}, function(data){
					  loc.formartLng = data.lng
					  loc.formartLat = data.lat
					  _this.getAddressAfter(loc);
				  });
			  }else{
				  _this.getAddressAfter(loc);
			  }  
			}
		},
		getAddressAfter(loc){
			// 缓存到本地 可供其它H5页面使用
			window.localStorage.setItem('txMapAddress', JSON.stringify(loc))
			
			console.log('loc', loc);
			app.globalData.myMapAddress = loc;
			common.navigateBack()
		}
	},
	components: {}
};
</script>

<style lang="stylus">
.open-map
	height 100%
</style>
